<template>
  <el-form ref="deptForm" :model="form" :rules="rules" label-width="120px">
    <el-form-item label="部门名称" prop="name">
      <el-input v-model="form.name" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门编码" prop="code">
      <el-input v-model="form.code" style="width:80%" placeholder="1-50个字符" />
    </el-form-item>
    <el-form-item label="部门负责人" prop="manager">
      <el-select v-model="form.manager" style="width:80%" placeholder="请选择">
        <el-option v-for="item in employee" :key="item.id" :value="item.username" :label="item.username" />
      </el-select>
    </el-form-item>
    <el-form-item label="部门介绍" prop="introduce">
      <el-input v-model="form.introduce" style="width:80%" placeholder="1-300个字符" type="textarea" :rows="3" />
    </el-form-item>
    <el-form-item>
      <el-button type="primary" size="small" @click="hSubmit">确定</el-button>
      <el-button size="small" @click="hCancel">取消</el-button>
    </el-form-item>
  </el-form>
</template>

<script>
import { getEmployee } from '@/api/employee'
import { addDepartments } from '@/api/adddepart'
import { inquireDepartments, putDepartments } from '@/api/departments'
export default {
  props: {
    mwj: {
      type: String,
      required: true
    },
    isedit: {
      type: Boolean,
      required: true
    },
    originList: {
      type: Array,
      required: true
    }
  },
  data() {
    const validcode = (rule, value, callback) => {
      // const existCodeList = this.originList.map(item => item.code)

      let codeList = null

      // 编辑状态的特殊处理: 将当前正在编辑的部门从数组中给删掉 (不要修改原数组)
      // 编辑状态: isEdit
      // 当前正在编辑的部门: id
      if (this.isedit) {
        // filter: 过滤出「除了」正在编辑的部门以外的所有部门
        codeList = this.originList.filter(item => item.id !== this.mwj).map(({ code }) => code) // 将正在编辑的部门给排除在外
      } else {
        codeList = this.originList.map(({ code }) => code)
      }
      codeList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    const validateName = (rule, value, callback) => {
      // console.log('用户输入的部门名称:', value)
      // 需求: 部门名称统计不允许重复
      // 拆解需求:
      // 1. 添加时不允许和子部门名称重复
      // 2. 编辑时不允许和兄弟部门名称重复

      // 解决需求1: 找到当前点击部门的子部门, 判断是否在其中
      // this.id => 当前点击的部门
      let nameList = null
      if (this.isedit) {
        const pid = this.originList.find(item => item.id === this.mwj).pid
        nameList = this.originList.filter(item => item.pid === pid && item.id !== this.mwj).map(({ name }) => name)
        console.log(nameList)
      } else {
        nameList = this.originList.filter(item => item.pid === this.mwj).map(({ name }) => name)
      }

      // if(this.isedit){
      //   const pid = this.originList.find
      // }
      // console.log(nameList)
      nameList.includes(value) ? callback(new Error(value + '已存在')) : callback()
    }
    return {
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      employee: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2~10个字符', trigger: 'blur' },
          { validator: validateName, trigger: 'blur' }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: 'blur' },
          { min: 2, max: 50, message: '请输入2~50个字符', trigger: 'blur' },
          { validator: validcode, trigger: 'blur' }
        ],
        manager: [
          { required: true, message: '请选择部门负责人', trigger: 'change' }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: 'blur' },
          { min: 2, max: 300, message: '请输入2~300个字符', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    this.getEmpoyees()
    this.editDepartments()
  },
  methods: {
    async editDepartments() {
      if (!this.isedit) return
      const { data: res } = await inquireDepartments(this.mwj)
      this.form = res.data
    },
    hSubmit() {
      this.$refs.deptForm.validate(async valid => {
        if (!valid) return
        let res = null
        try {
          if (!this.isedit) {
            this.form.pid = this.mwj
            res = await addDepartments(this.form)
          } else {
            res = await putDepartments(this.form)
          }
          this.$message.success(res.data.message)
          this.$emit('success')
        } catch (e) {
          console.log(e)
        }
      })
    },
    hCancel() {
      this.$emit('success')
    },
    async getEmpoyees() {
      try {
        const { data: res } = await getEmployee()
        this.employee = res.data
      } catch (e) {
        console.log(e)
      }
    }
  }
}
</script>
